package com.ml.mall.service.impl;

import com.ml.mall.bean.page.Page;
import com.ml.mall.bean.req.BaseDateRangeReq;
import com.ml.mall.bean.req.admin.category.AdminCategoryQueryPageReq;
import com.ml.mall.bean.req.admin.purchase.PurchaseQueryOneReq;
import com.ml.mall.bean.req.admin.purchase.PurchaseQueryReportPageReq;
import com.ml.mall.bean.req.admin.supplier.SupplierQueryOneReq;
import com.ml.mall.bean.res.admin.category.AdminCategoryQueryPageRes;
import com.ml.mall.bean.res.admin.supplier.SupplierQueryOneRes;
import com.ml.mall.bean.res.admin.supplier.vo.SupplierQueryOneVO;
import com.ml.mall.bean.res.partner.PartnerRegisterCompanyRes;
import com.ml.mall.bean.res.partner.PartnerSupplierQueryPageRes;
import com.ml.mall.bean.res.partner.PartnerSupplierQueryReportRes;
import com.ml.mall.bean.vo.DateValueVO;
import com.ml.mall.consts.UserRoleTypeConsts;
import com.ml.mall.entity.sys.SysUser;
import com.ml.mall.security.JwtUtil;
import com.ml.mall.service.AnalyticsAdminService;
import com.ml.mall.service.AnalyticsPartnersService;
import com.ml.mall.service.asyn.AnalyticsPartnerServiceAsyn;
import com.ml.mall.utils.DateTransformUtils;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

/**
 * Created by kyyang on 2021/6/28 23:20
 */
@Service
public class AnalyticsPartnersServiceImpl implements AnalyticsPartnersService {


    @Autowired
    private AnalyticsPartnerServiceAsyn analyticsPartnerServiceAsyn;

    @Autowired
    private AnalyticsAdminService analyticsAdminService;


    @Override
    public PartnerRegisterCompanyRes getPartnersRegisterCompany(BaseDateRangeReq req) throws Exception{

        PartnerRegisterCompanyRes res = new PartnerRegisterCompanyRes();

        SysUser currentUser = JwtUtil.getCurrentUser();

        if(currentUser!=null&& UserRoleTypeConsts.ROLE_TYPE_2==currentUser.getRoleType()){
            //查询时间范围内 销售额
            Future<BigDecimal> totalSalesFuture =  analyticsPartnerServiceAsyn.getPartnersRegisterCompanySales(req,currentUser.getCompanyId());
            //查询时间范围内注册数量
            Future<List<DateValueVO>> listFuture = analyticsPartnerServiceAsyn.getPartnersRegisterCompanyListRegister(req,currentUser.getCompanyId());
            //查询时间范围内卖家销售情况
            Future<List<DateValueVO>> listSalesFuture = analyticsPartnerServiceAsyn.getPartnersSupplierCompanyListSales(req,currentUser.getCompanyId());

            if(totalSalesFuture!=null){
                BigDecimal bigDecimal = totalSalesFuture.get();
                res.setTotalSales(bigDecimal);
            }
            if(listFuture!=null){
                List<DateValueVO> dateValueVOS = listFuture.get();
                Map<String, Double> collect = dateValueVOS.stream().collect(Collectors.groupingBy(e -> e.getYmd(), Collectors.summingDouble(g -> g.getValue().doubleValue())));
                List<DateValueVO> curDateMonthDataInit = DateTransformUtils.getCurDateMonthDataInit(req.getTimeBegin(), req.getTimeEnd(), collect);
                res.setDayDetail(curDateMonthDataInit);
            }
            if(listSalesFuture!=null){
                List<DateValueVO> dateValueVOS = listSalesFuture.get();
                Map<String, Double> collect = dateValueVOS.stream().collect(Collectors.groupingBy(e -> e.getYmd(), Collectors.summingDouble(g -> g.getValue().doubleValue())));
                List<DateValueVO> curDateMonthDataInit = DateTransformUtils.getCurDateMonthDataInit(req.getTimeBegin(), req.getTimeEnd(), collect);
                res.setSalesDayDetail(curDateMonthDataInit);
            }

        }
        return res;
    }

    @Override
    public List<PartnerSupplierQueryReportRes> partnerSupplierQueryReport(BaseDateRangeReq req) throws Exception {
        SysUser currentUser = JwtUtil.getCurrentUser();
        if(currentUser!=null&& UserRoleTypeConsts.ROLE_TYPE_2==currentUser.getRoleType()){
            Future<List<DateValueVO>> listFuture = analyticsPartnerServiceAsyn.getPartnerSupplierQueryReport(req,currentUser.getCompanyId());
            if(listFuture!=null){
                List<DateValueVO> dateValueVOS = listFuture.get();
                Map<String, List<DateValueVO>> collect = dateValueVOS.stream().collect(Collectors.groupingBy(DateValueVO::getName));
                List<PartnerSupplierQueryReportRes> list = new ArrayList<>();
                for(Map.Entry<String, List<DateValueVO>> entry:collect.entrySet()){
                    String key = entry.getKey();
                    List<DateValueVO> value = entry.getValue();
                    PartnerSupplierQueryReportRes res = new PartnerSupplierQueryReportRes();
                    res.setCompanyName(key);
                    res.setTotalSales(BigDecimal.valueOf(value.stream().mapToDouble(e->e.getValue().doubleValue()).sum()));
                    Map<String, Double> collect1 = value.stream().collect(Collectors.groupingBy(e -> e.getYmd(), Collectors.summingDouble(g -> g.getValue().doubleValue())));
                    List<DateValueVO> curDateMonthDataInit = DateTransformUtils.getCurDateMonthDataInit(req.getTimeBegin(), req.getTimeEnd(), collect1);
                    res.setDayDetail(curDateMonthDataInit);
                    list.add(res);
                }
                return list;
            }
        }
        return Collections.emptyList();
    }

    @Override
    public Page<PartnerSupplierQueryPageRes> partnerSupplierQueryPage(PurchaseQueryReportPageReq req) throws Exception {
        SysUser currentUser = JwtUtil.getCurrentUser();
        Page<PartnerSupplierQueryPageRes> res = new Page<PartnerSupplierQueryPageRes>(req.getPageIndex(), req.getPageNum());
        if (currentUser != null && UserRoleTypeConsts.ROLE_TYPE_2 == currentUser.getRoleType()) {
            int count = 0;
            Future<Integer> countFuture = analyticsPartnerServiceAsyn.getPartnerSupplierQueryPageCount(req,currentUser.getCompanyId());
            if (countFuture != null) {
                count = countFuture.get();
            }
            res.setTotal(count);
            if (count > 0) {
                Future<List<PartnerSupplierQueryPageRes>> purchaseReportPageResFuture = analyticsPartnerServiceAsyn.getPartnerSupplierQueryPageList(req,currentUser.getCompanyId());
                if (purchaseReportPageResFuture != null) {
                    List<PartnerSupplierQueryPageRes> purchaseReportPageRes = purchaseReportPageResFuture.get();
                    res.setRecords(purchaseReportPageRes);
                }
            }
        }
        return res;
    }

    @Override
    public SupplierQueryOneRes partnerSupplierQueryOneReport(SupplierQueryOneReq req) throws Exception {
        SupplierQueryOneRes supplierQueryOneRes = analyticsAdminService.adminSupplierQueryOne(req);
        return supplierQueryOneRes;
    }

    @Override
    public Page<AdminCategoryQueryPageRes> partnerSupplierQueryCategorySalesPage(AdminCategoryQueryPageReq req) throws Exception {
        Page<AdminCategoryQueryPageRes> res = new Page<AdminCategoryQueryPageRes>(req.getPageIndex(),req.getPageNum());
        SysUser currentUser = JwtUtil.getCurrentUser();
        if (currentUser != null && UserRoleTypeConsts.ROLE_TYPE_2 == currentUser.getRoleType()) {
            int count = 0;
            Future<Integer> countFuture = analyticsPartnerServiceAsyn.selectPartnerCategoryQueryPageCount(req.getTimeBegin(), req.getTimeEnd(), req.getCategoryName(),currentUser.getCompanyId());
            if (countFuture != null) {
                count = countFuture.get();
            }
            res.setTotal(count);
            if (count > 0) {
                Future<List<AdminCategoryQueryPageRes>> purchaseReportPageResFuture = analyticsPartnerServiceAsyn.selectPartnerCategoryQueryPageList(req.getTimeBegin(), req.getTimeEnd(), req.getSortType(), req.getCategoryName(),currentUser.getCompanyId(), req.getStart(), req.getEnd());
                if (purchaseReportPageResFuture != null) {
                    List<AdminCategoryQueryPageRes> purchaseReportPageRes = purchaseReportPageResFuture.get();
                    res.setRecords(purchaseReportPageRes);
                }
            }
        }
        return res;
    }
}
